package com.hb.trade.api.push.task.tenderprojectphase;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hb.core.pojo.OrganizeInfo;
import com.hb.core.pojo.center.sys.entity.DistrictInfo;
import com.hb.core.pojo.project.proj.entity.AbstractProject;
import com.hb.core.pojo.project.proj.enums.BidWayEnum;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.api.push.util.ConstantUtil;
import com.hb.trade.client.center.DictionaryClient;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.fzzn.core.util.Enums;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe:
 *
 * @author zhongdl
 * @date 2022/02/17
 */
@Data
public class TenderProjectPhase {

    @JsonProperty("PROJECT")
    private Project project;


    /**
     *项目编号
     */
    @JsonProperty("TENDER_PROJECT_CODE")
    private String tenderProjectCode;
    /**
     *项目名称
     */
    @JsonProperty("TENDER_PROJECT_NAME")
    private String tenderProjectName;
    /**
     *招标项目行业分类编码
     */
    @JsonProperty("TENDER_PROJECT_INDUSTRIES_TYPE")
    private String tenderProjectIndustriesType;
    /**
     *项目所在行政区域代码
     */
    @JsonProperty("REGION_CODE")
    private String regionCode;
    /**
     *招标内容与范围及招标方案说明
     */
    @JsonProperty("TENDER_CONTENT")
    private String tenderContent;

//      /**
//     * 没有标注 默认传0
//     */
//    @JsonProperty("ISUNION")
//    private Integer isUnion;

    @JsonProperty("TENDERERS")
    private List<Map<String,Tenderer>> tenderers;

    @JsonIgnore
    private List<Tenderer> tendererIgnores;



    @JsonProperty("TENDER_AGENCYS")
    private List<Map<String,TenderAgency>> tenderAgencies;

    /**
     *招标方式
     */
    @JsonProperty("TENDER_MODE")
    private String tenderMode;
    /**
     *招标组织形式
     */
    @JsonProperty("TENDER_ORGANIZE_FORM")
    private String tenderOrganizeForm;
    /**
     *招标项目建立时间
     */
    @JsonProperty("CREATE_TIME")
    private String createTime;

    /**
     *监督部门代码
     */
    @JsonProperty("SUPERVISE_DEPT_CODE")
    private String superviseDeptCode;

    /**
     *监督部门名称
     */
    @JsonProperty("SUPERVISE_DEPT_NAME")
    private String superviseDeptName;
    /**
     *审核部门代码
     */
    @JsonProperty("APPROVE_DEPT_CODE")
    private String approveDeptCode;

    /**
     *审核部门名称
     */
    @JsonProperty("APPROVE_DEPT_NAME")
    private String approveDeptName;

    /**
     *申报责任人
     */
    @JsonProperty("REPORT_DUTY")
    private String reportDuty;

    /**
     *标段列表
     */
    @JsonIgnore
    private List<BidSectionPush> bidSectionIgnores;
    /**
     *标段列表
     */
    @JsonProperty("BID_SECTIONS")
    private BidSectionPushS bidSections;

    /**
     *交易平台或发布工具标识码
     */
    @JsonProperty("TRADE_PLAT")
    private String tradePlat;
    /**
     *公共服务平台标识码
     */
    @JsonProperty("PUB_SERVICE_PLAT_CODE")
    private String pubServicePlatCode;
    /**
     *数据时间戳
     */
    @JsonProperty("SUBMIT_TIMESTAMP")
    private String submitTimestamp;

    public TenderProjectPhase() {
    }

    public TenderProjectPhase(AbstractProject project) {
        //封装项目
        Project proj = new Project();
        proj.setProjectName(project.getProjectName());
        proj.setProjectCode(project.getTenderProjectCode().substring(0,17));
        //地区行政码
        DictionaryClient dictionaryClient = SpringUtil.getBean(DictionaryClient.class);
        // 数据库台湾省只有省没有地区
        String district = null == project.getArea() ? project.getProvince() : project.getArea();
        DistrictInfo districtInfo = dictionaryClient.districtsById(district);
        proj.setRegionCode(districtInfo.getAreaCode());
        proj.setIndustriesType(project.getIndustryType());
        //资金来源
        proj.setFundSource("/");
        //项目规模
        proj.setProjectScale("/");

        setProject(proj);
        setTenderProjectCode(project.getTenderProjectCode());
        setTenderProjectName(project.getProjectName());
        setTenderProjectIndustriesType(transformIndustriesType(project.getIndustryType()));
        setRegionCode(districtInfo.getAreaCode());
        setTenderContent("/");


        OrganizeInfo biddingOrganizeInfo = project.getBiddingOrganizeInfo();
        List<Tenderer> tendererIgnores = CollUtil.list(false);
        Tenderer tenderer = new Tenderer();
        tenderer.setTenderCodeType("05");
        tenderer.setTendererCode(StrUtil.isEmpty(biddingOrganizeInfo.getCreditCode()) ? "/" : biddingOrganizeInfo.getCreditCode());
        tenderer.setTendererName(StrUtil.isEmpty(biddingOrganizeInfo.getName()) ? "/" : biddingOrganizeInfo.getName());
        tendererIgnores.add(tenderer);

        setTendererIgnores(tendererIgnores);

        List<Map<String,Tenderer>> tenderers = CollUtil.list(false);
        for (Tenderer tendererIgnore : tendererIgnores) {
            HashMap<String, Tenderer> map = MapUtil.newHashMap(1);
            map.put("TENDERER",tendererIgnore);
            tenderers.add(map);
        }
        setTenderers(tenderers);

        List<Map<String,TenderAgency>> tenderAgencies = CollUtil.list(false);
        Map<String, TenderAgency> objectObjectHashMap = MapUtil.newHashMap(1);
        OrganizeInfo agencyOrganizeInfo = project.getAgencyOrganizeInfo();

        TenderAgency tenderAgency = new TenderAgency();
        tenderAgency.setTenderAgencyCodeType("05");
        tenderAgency.setTenderAgencyCode(StrUtil.isEmpty(agencyOrganizeInfo.getCreditCode()) ? "/" : agencyOrganizeInfo.getCreditCode());
        tenderAgency.setTenderAgencyName(StrUtil.isEmpty(agencyOrganizeInfo.getName()) ? "/" : agencyOrganizeInfo.getName());

        objectObjectHashMap.put("TENDER_AGENCY",tenderAgency);
        tenderAgencies.add(objectObjectHashMap);
        setTenderAgencies(tenderAgencies);

        setTenderMode(Enums.equals(BidWayEnum.BID,project.getBidWay()) && project.getCommonType() != null ? project.getCommonType() + "" : "9");
        setTenderOrganizeForm(Integer.valueOf(1).equals(project.getPurchaseType()) ? "2" : "1");
        setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        setSuperviseDeptCode("/");
        setSuperviseDeptName("/");
        setApproveDeptCode("/");
        setApproveDeptName("/");
        setReportDuty("/");

        List<BidSectionDTO> sections = project.getSections();
        List<BidSectionPush> bidSectionPushes = CollUtil.list(false);
        int size = sections.size();
        for (int i = 0; i < size; i++) {
            BidSectionDTO section = sections.get(i);
            BidSectionPush bidSectionPush = new BidSectionPush();
            bidSectionPush.setBidSectionCode(section.getSectionCode());
            bidSectionPush.setBidSectionName(section.getSectionName());
            bidSectionPush.setBidSectionContent("/");
            //TODO 标段包分类代码
            bidSectionPush.setBidSectionClassifyCode("B012199");
            bidSectionPush.setContractReckonPrice(section.getEstimatedContractPrice() == null ? BigDecimal.ZERO : section.getEstimatedContractPrice());
            bidSectionPush.setCurrencyCode(156);
            bidSectionPush.setBiddingEligibility("/");
            bidSectionPushes.add(bidSectionPush);
        }
        BidSectionPushS bidSectionPushS = new BidSectionPushS();
        bidSectionPushS.setBidSectionPushList(bidSectionPushes);
        setBidSections(bidSectionPushS);
        setTradePlat(ConstantUtil.SOURCE_SYSTEM_ID);
        setPubServicePlatCode(ConstantUtil.TARGET_SYSTEM_ID_TEST);
        setSubmitTimestamp(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));

    }
    /*
     *
     * @Description: 行业分类转换
     * @Author: zhongdl
     * @Date:2022/2/22 17:27
     * @param type:
     * @return: {@link java.lang.String }
     **/
    private String transformIndustriesType(String type) {
        String transformType = "";
        switch (type) {
            case "A01":
                transformType = "G01";
                break;
            case "A02":
                transformType = "G02";
                break;
            case "A03":
                transformType = "G03";
                break;
            case "A04":
                transformType = "G04";
                break;
            case "A05":
                transformType = "G05";
                break;
            case "A06":
                transformType = "G06";
                break;
            case "A07":
                transformType = "G07";
                break;
            case "A08":
                transformType = "G08";
                break;
            case "A09":
                transformType = "G09";
                break;
            case "A12":
                transformType = "G16";
                break;
            case "A13":
                transformType = "G20";
                break;
            default:
                transformType = "G99";
        }
        return transformType;
    }

}
